package com.sx.pipeline.ui.bank

import android.app.Application
import android.text.TextUtils
import com.blankj.utilcode.util.ToastUtils
import com.sx.mvvmlibs.base.BaseViewModel
import com.sx.mvvmlibs.event.SingleLiveEvent
import com.sx.mvvmlibs.http.CommonObserver
import com.sx.mvvmlibs.utils.Logs
import com.sx.mvvmlibs.utils.RxThreadHelper
import com.sx.pipeline.R
import com.sx.pipeline.app.config.OperatorNeedsType
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.repos.http.req.AddBankDTO
import com.sx.pipeline.repos.http.req.AddInquiryDTO
import com.sx.pipeline.repos.http.req.AddQuotedPriceDTO
import com.sx.pipeline.repos.http.req.AuditTaskDTO
import com.sx.pipeline.repos.http.res.*

/**
 *    author : zhouyouqiang
 *    e-mail : 1638473898@qq.com
 *    date   : 2022/5/20 13:51
 *    desc   :
 *    version: 1.0
 */
class BankVM(app: Application) : CYBaseViewModel(app) {


    /**
     * 添加银行卡
     */
    val auditSuccessEvent: SingleLiveEvent<Boolean> = SingleLiveEvent()
    fun addBankCard(
        userName: String,
        identityCard: String,
        bankAccount: String,
        bank: String,
        phone: String,
    ) {
        if (TextUtils.isEmpty(userName)) {
            ToastUtils.showShort("请填写持卡人姓名")
            return
        }
        if (TextUtils.isEmpty(identityCard)) {
            ToastUtils.showShort("请填写身份证号码")
            return
        }
        if (TextUtils.isEmpty(bankAccount)) {
            ToastUtils.showShort("请填写银行卡卡号")
            return
        }
        if (TextUtils.isEmpty(bank)) {
            ToastUtils.showShort("请填写开户行")
            return
        }
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.showShort("请填写预留手机号")
            return
        }
        val dto = AddBankDTO(userName, identityCard, bankAccount, bank, phone)
        addSubscribe(
            mModel.addBankCard(dto)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Any, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: Any) {
                        ToastUtils.showShort("添加成功")
                        finish()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }

    /**
     * 获取银行卡列表
     */
    // 银行卡列表
    val bankListEvent: SingleLiveEvent<Ipage<BankListVO>> = SingleLiveEvent()
    fun getBankCardList(page: Int = 1) {
        addSubscribe(
            mModel.getBankCardList(page)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Ipage<BankListVO>, BaseViewModel<*>>
                    (this, false) {
                    override fun onResult(result: Ipage<BankListVO>) {
                        bankListEvent.postValue(result)
                        mUI.initRefreshEvent().postValue(true)
                        if (page != 1) {
                            mUI.initLoadEvent().postValue(true)
                        }
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort(msg)
                        mUI.initRefreshEvent().postValue(false)
                        if (page != 1) {
                            mUI.initLoadEvent().postValue(true)
                        }
                    }
                })
        )
    }


    /**
     * 解绑银行卡
     */
    fun deleteBank(id: Int) {
        addSubscribe(
            mModel.deleteBank(id)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Any, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: Any) {
                        ToastUtils.showShort("解绑成功")
                        getBankCardList(1)
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }
}